package com.unidt.www.module.bpm.service.task.impl;

import cn.hutool.core.date.DateUtil;
import com.unidt.www.module.bpm.dal.mysql.definition.ActReActConfigMapper;
import com.unidt.www.module.bpm.service.definition.BpmProcessDefinitionService;
import com.unidt.www.module.bpm.service.task.BpmTaskNotifyService;
import com.unidt.www.module.system.dal.dataobject.user.AdminUserDO;
import com.unidt.www.module.system.dal.dataobject.user.SystemZzdUser;
import com.unidt.www.module.system.dal.mysql.user.AdminUserMapper;
import com.unidt.www.module.system.dal.mysql.user.SystemZzdUserMapper;
import com.unidt.www.module.third.api.workbar.IWorkMessageApi;
import com.unidt.www.module.third.api.workbar.dto.MessageBody;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author sk
 * @Description:
 * @Date 2022/12/9 23:11
 **/
@Data
@Service
public class BpmTaskNotifyServiceImpl implements BpmTaskNotifyService {

    @Resource
    private AdminUserMapper userMapper;

    @Resource
    private SystemZzdUserMapper zzdUserMapper;

    @Resource
    IWorkMessageApi workMessageApi;

    @Autowired
    ActReActConfigMapper actReActConfigMapper;

    @Resource
    private BpmProcessDefinitionService processDefinitionService;

    @Override
    @Async
    public void notify(Task task, FlowableEngineEventType eventType) {
        /**
         * 发送消息
         */
        notifyMessage(task,eventType);

    }


    /**
     * 功能描述: 推送消息
     * @author sk
     * @date 2023/2/7
     * @param task
     * @param eventType
     * @return void
     */
    private void notifyMessage(Task task,FlowableEngineEventType eventType){
        String assignee = task.getAssignee();
        if (StringUtils.isNotBlank(assignee)) {
            Long userId = Long.valueOf(assignee);
            AdminUserDO adminUserDO = userMapper.selectById(userId);
            Integer userType = adminUserDO.getUserType();
            /**
             * 工作台用户 发送代办消息
             */
            if (Integer.valueOf("1").equals(userType)) {
                ProcessDefinition processDefinition = this.processDefinitionService.getProcessDefinition(task.getProcessDefinitionId());
                String contentTemplate = "您有一条新的待办通知,流程名称{0},当前任务名称{1}，任务开始时间{2},请及时处理";
                String processName = processDefinition.getName();
                String taskName = task.getName();
                String startTime = DateUtil.formatTime(new Date());
                SystemZzdUser zzdUser = this.zzdUserMapper.getByUserId(userId);

                if (Objects.nonNull(zzdUser)) {

                    if (FlowableEngineEventType.TASK_CREATED.equals(eventType)) {
                        MessageBody messageBody = new MessageBody();
                        List<String> receiverIds = new ArrayList<>();
                        receiverIds.add(zzdUser.getZzdCode());
                        String title = "经信业务流程待办";
                        String content = MessageFormat.format(contentTemplate, new String[]{processName, taskName, startTime});
                        messageBody.setTitle(title);
                        messageBody.setMsgType(String.valueOf(2));
                        messageBody.setContent(content);
                        messageBody.setReceiverIds(receiverIds);

                        this.workMessageApi.sendMessage(messageBody,task.getId());

                    } else if (FlowableEngineEventType.TASK_COMPLETED.equals(eventType) ||FlowableEngineEventType.ACTIVITY_CANCELLED.equals(eventType)) {

                        /**
                         * 完成和取消全部 将工作台消息 改写成已处理
                         */

                        this.workMessageApi.endMessage(zzdUser.getZzdCode(),task.getId());


                    }
                }
            }
        }
    }


}
